<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Systemset extends CI_Controller {
	
	private $tab = 'system';
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
	}

	
	function sysparameter(){
		$ftype 			= intval($this->input->get('ftype'));
		if($ftype != 0){
			$data['where']['ftype'] = $ftype;
		}
		$per_page 	= $this->input->get('per_page')?$this->input->get('per_page'):0;
		
		$this->load->model('systemset_model');
		
		$data['page_size']  = YTZ_LIST_PAGE_SIZE;
		$data['page_start'] = (int)$per_page;
		
		$vdata = $this->systemset_model->systemsetlist($data);
		
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/systemset/sysparameter/?ftype='.$ftype;
		$config['total_rows'] = $vdata['sys_count'];
		$config['per_page'] = YTZ_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['tab'] = $this->tab;
		$sysparam = array(1=>'webparameter',2=>'borrowparameter',3=>'picparameter',4=>'emailparameter',5=>'loginparameter',6=>'messageparameter');
		$vdata['sub'] = $sysparam[$ftype];
		
		$this->load->view('/ychadmin/systemset_list', $vdata);
	}
	
	function addparameter(){
		$this->load->model('systemset_model');
		$act = $this->input->post('act');
		$this->load->model('log_model');
		
		if(isset($act) && $act){
			
			if($act == 'add'){
				$data = array(
					'name'  => $this->input->post('name'),
					'nid' 		 => $this->input->post('nid'),
					'fvalue'        => $this->input->post('fvalue'),
					'ftype'  => $this->input->post('ftype'),
				);
				
				$systemset_id = $this->systemset_model->add($data);
				$this->log_model->add_log('[添加][系统管理设置]->['.$data['name'].']');
				$this->create_system_file();
				header("location:/ychadmin/systemset/sysparameter/?ftype=".$data['ftype']);
				exit;
			}else if($act == 'edit'){
				$id = 	$this->input->post('id');
				$sysinfo = $this->systemset_model->get_systemset(array('id'=>$id));
				$edata = array(
					'fvalue'        => $this->input->post('fvalue'),
				);
				$res = $this->systemset_model->update($id,$edata);
				
				if($res>0){
					$this->log_model->add_log('[编辑][系统管理设置]->['.$sysinfo['name'].']');
					$ret = array('msg'=>'操作成功');
				}else{
					$ret = array('msg'=>'操作失败');
				}
			}else if($act =='delete'){
				$id = 	$this->input->post('id');
				$sysinfo = $this->systemset_model->get_systemset(array('id'=>$id));
				
				$ddata = array('is_del'=>1);
				$res = $this->systemset_model->update($id,$ddata);
				if($res>0){
					$this->log_model->add_log('[删除][系统管理设置]->['.$sysinfo['name'].']');
					$ret = array('msg'=>'操作成功');
				}else{
					$ret = array('msg'=>'操作失败');
				}
				
			}	
			
			$this->create_system_file();
			echo json_encode($ret);
			exit; 
	   		
		}else{
			$vdata['tab'] = $this->tab;
			$vdata['sub'] = 'addparameter';
		
			$vdata['sys_type'] 				= $this->systemset_model->arrays('sys_type');
			$this->load->view('/ychadmin/systemset_add', $vdata);
		}
 		
	}
	
	
	function sys_synchronize(){
		$this->load->model('systemset_model');
		$id = $this->input->post("id");
		$sysinfo = $this->systemset_model->get_systemset(array('id'=>$id));
		$sys_type= $this->systemset_model->arrays('sys_type');
		$sysinfo['ftype'] = $sys_type[$sysinfo['ftype']];
		
		$ret = array('msg'=>$sysinfo);
		echo json_encode($ret);
		exit; 
	}
	
	
	
	
	/**
	 * 系统配置
	 * @param $id
	 */
	public function edit_view(){
		$this->load->helper(array('form'));
        $this->load->model('systemset_model');
		$isset = $this->systemset_model->check_systemset();
		
		$vdata['tab'] 	= $this->tab;
		$vdata['sub'] 	= 'systemset';
		
		if(false == $isset) {
			$this->load->view("/ychadmin/systemset_add",$vdata);
			return;
		}
  		$systemset = $this->systemset_model->get_systemset();
  		$vdata['systemset'] = $systemset;
  		
		
  		$this->load->view("/ychadmin/systemset_edit",$vdata);
	}
	
	public function add(){

		$data = array(
			'platform_name'  => $this->input->post('platform_name'),
			'web_name' 		 => $this->input->post('web_name'),
			'web_url'        => $this->input->post('weburl'),
			'webmobile_url'  => $this->input->post('webmobileurl'),
			'webbbs_url'     => $this->input->post('webbbsurl'),
			'keywords'       => $this->input->post('keywords'),
			'description'    => $this->input->post('description'),
			'fuwutel400'	 => $this->input->post('fuwutel400'),
			'fuwutel'        => $this->input->post('fuwutel'),
			'compay_name'    => $this->input->post('compay_name'),
			'fax'            => $this->input->post('fax'),
			'address'        => $this->input->post('address')
		);

		if(!file_exists(UPFILES_LOGO_IMG)){
			mkdir(UPFILES_LOGO_IMG);
		}
		$logoimg = "";
		if($_FILES['logoimg']['name'] != ''){ 
			$logoimg = sha1($_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR'].time().rand());
			$tmpfn = $_FILES['logoimg']['name'];
			$pt=strrpos($tmpfn, ".");
			$retval=substr($tmpfn, $pt+1, strlen($tmpfn) - $pt);
			$logoimg.=".".$retval;
			$upfile = UPFILES_LOGO_IMG."/".$logoimg;
			if(is_uploaded_file($_FILES['logoimg']['tmp_name']))
				move_uploaded_file($_FILES['logoimg']['tmp_name'],$upfile);
		}
		
		
  		$data['logoimg'] = $logoimg;
		$this->load->model('systemset_model');
  		$systemset_id = $this->systemset_model->add($data);
		
		$this -> create_file($data);
  		
  		header("location:/ychadmin/systemset/edit_view/");
		$this->load->model('log_model');$this->log_model->add_log('[添加][系统管理设置]['.$data['platform_name'].']');
   		exit;
	}

	function template_file(){
		$vdata['tab'] 	= $this->tab;
		$vdata['sub'] 	= 'systemfile';
  		$this->load->view("/ychadmin/systemset_template",$vdata);
	}
	
	function create_system_file(){
		$this->load->model('systemset_model');
		$data['limit'] = 'all';
		$vdata = $this->systemset_model->systemsetlist($data);
		$isystem_list = $vdata['sys_list'];
		
		$data = "<?php".PHP_EOL;
		foreach($isystem_list as $isystem){
			$data .="define('".strtoupper($isystem['nid'])."', '".$isystem['fvalue']."');".PHP_EOL;
		}
		
		$this->load->helper('file');
		if(!write_file('./application/config/constants_site.php', $data,'w+')){
			error_log("Unable to write the file",3,"/var/www/html/test.yunchsoft.com/no.log");
		}else{
			write_file('./application/config/constants_site.php', $data,'w+');
		}
		
		//echo 'SUCCESS';
		
	}
	
	/**
	 * 创建文件
	 * @param $cont
	 */
	 //*
	private function create_file($cont){
		foreach ($cont as $k => $v){
			$$k = $v;
		}

		$data ="<?php".PHP_EOL
		."define('CON_PLATFORMNAME', 		'$platform_name');//平台名称".PHP_EOL
		."define('CON_WEBNAME', 			'$web_name');//网站名称".PHP_EOL
		."define('CON_WEBURL', 				'http://$web_url');//网站网址".PHP_EOL
		."define('CON_WEBURL1', 			'$web_url');//网站网址".PHP_EOL
		."define('CON_WEBURL2', 			'$web_url');//网站网址".PHP_EOL
		."define('CON_WEBBBSURL', 			'http://$webbbs_url');//bbs网址".PHP_EOL
		."define('CON_WEBBBSURL1', 			'$webbbs_url');//bbs网址".PHP_EOL
		."define('CON_WEBMOBILEURL', 		'http://$webmobile_url');//手机端网址".PHP_EOL
		."define('CON_WEBMOBILEURL1', 		'$webmobile_url');//手机端网址".PHP_EOL
		."define('CON_KEYWORDS', 			'$keywords');//网站关键词".PHP_EOL
		."define('CON_DESCRIPTION', 		'$description');//网站描述".PHP_EOL
		."define('CON_BEIAN', 				'$beian');//网站备案号".PHP_EOL
		."define('CON_400FUWUTEL', 			'$fuwutel400');//客户服务热线".PHP_EOL
		."define('CON_FUWUTEL', 			'$fuwutel');//客户服务热线".PHP_EOL
		."define('CON_FAX', 				'$fax');//传真".PHP_EOL
		."define('CON_ADDRESS', 			'$address');//联系地址".PHP_EOL
		."define('COMPANY_NAME',            '$compay_name');//公司名称".PHP_EOL;

		$this->load->helper('file');
		if(!write_file('./application/config/constants_site.php', $data,'w+')){
			error_log("Unable to write the file",3,"/var/ftp/no.log");
		}else{
			write_file('./application/config/constants_site.php', $data,'w+');
		}
	}
	
	public function edit(){
		$id          = $this->input->post('id');
		$oldlogoimg  = $this->input->post('oldlogoimg');
		$data = array(
			'platform_name'  => $this->input->post('platform_name'),
			'web_name' 		 => $this->input->post('web_name'),
			'web_url'        => $this->input->post('weburl'),
			'webmobile_url'  => $this->input->post('webmobileurl'),
			'webbbs_url'     => $this->input->post('webbbsurl'),
			'keywords'       => $this->input->post('keywords'),
			'description'    => $this->input->post('description'),
			'fuwutel400'	 => $this->input->post('fuwutel400'),
			'fuwutel'        => $this->input->post('fuwutel'),
			'compay_name'    => $this->input->post('compay_name'),
			'fax'            => $this->input->post('fax'),
			'beian'          => $this->input->post('beian'),
			'address'        => $this->input->post('address')
		);

		if(!file_exists(UPFILES_LOGO_IMG)){
			mkdir(UPFILES_LOGO_IMG);
		}
		$logoimg = "";
		if($_FILES['logoimg']['name'] != ''){ 
			$logoimg = sha1($_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR'].time().rand());
			$tmpfn = $_FILES['logoimg']['name'];
			$pt=strrpos($tmpfn, ".");
			$retval=substr($tmpfn, $pt+1, strlen($tmpfn) - $pt);
			$logoimg.=".".$retval;
			$upfile = UPFILES_LOGO_IMG."/".$logoimg;
			if(is_uploaded_file($_FILES['logoimg']['tmp_name']))
				move_uploaded_file($_FILES['logoimg']['tmp_name'],$upfile);
		}

  		$logoimg         = $logoimg?$logoimg:$oldlogoimg;
		$data['logoimg'] = $logoimg;
		$this->load->model('systemset_model');
  		$systemset_id = $this->systemset_model->update($id,$data);
		
		$this -> create_file($data);
  		
  		header("location:/ychadmin/systemset/edit_view/".$id);
		$this->load->model('log_model');$this->log_model->add_log('[修改][系统管理设置]->'.$id.'->['.$data['platform_name'].']');
   		exit;
	}
}